import pymysql
import time
from com.atzl.common.UsersMessage import MessagesUsers
from com.atzl.functions.accounting.sql.AddData import AddDataToBase
from com.atzl.functions.accounting.func.GetSite import GetSite
from com.atzl.databaseconnection.MyDataToData import ToDataBase


# 监控用户模块
class CheckLogs(ToDataBase, GetSite):  # 继承to_data, GetSite

    # 监控用户动作,并将动作信息回传至数据库
    # 参数:
    #     timestamps:时间戳
    #     actions:用户动作
    #     operators:操作人
    def in_logs(self, timestamps, actions, operators):
        conn = self.creat_conn()
        cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
        sql = AddDataToBase.logs_messages(timestamps, actions, operators,  # 插入监控用户动作数据
                                          MessagesUsers.INTERNET_IP, MessagesUsers.LAN_IPv4,
                                          MessagesUsers.WLAN_IPv4, MessagesUsers.LAN_MAC,
                                          MessagesUsers.WLAN_MAC, MessagesUsers.PLACE_NATION,
                                          MessagesUsers.PLACE_STATE, MessagesUsers.PLACE_CITY)
        cur.execute(sql)  # 执行sql
        conn.commit()  # 提交
        cur.close()
        conn.close()



    # 监控用户动作,并将动作信息回传至数据库
    # 参数:
    #     actionstates:用户动作
    def logs_in(self, actionstates):
        the_time = str(time.strftime("%Y{y}%m{m}%d{d}%H{h}%M{f}%S{s}",  # 定义时间戳,并以xxxx年xx月xx日xx时xx分xx秒格式返回
                                     time.localtime())).format(y='年', m='月',
                                                               d='日', h='时',
                                                               s='秒', f='分')
        self.in_logs(the_time, actionstates, MessagesUsers.REAL_NAME)  # 监控用户动作,并将动作信息回传至数据库函数